<?php

namespace app\admin\service;

use app\admin\model\UserList;
use app\admin\model\UserWeb;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class UserService
{
    public static function goAdd()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'user_avatar' => 'require',
                'user_name' => 'require',
                'user_mobile' => 'require',
            ];
            $message = [
                'user_avatar.require' => "用户头像不能为空",
                'user_name.require' => "用户名称不能为空",
                'user_mobile.require' => "用户手机号不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            if ($data['user_birth']) $data['user_birth'] = strtotime($data['user_birth']);
            try {
                UserWeb::create($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'user_id' => 'require',
                'user_avatar' => 'require',
                'user_name' => 'require',
                'user_mobile' => 'require',
            ];
            $message = [
                'user_id.require' => "用户ID不能为空",
                'user_avatar.require' => "用户头像不能为空",
                'user_name.require' => "用户名称不能为空",
                'user_mobile.require' => "用户手机号不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            if ($data['user_birth']) $data['user_birth'] = strtotime($data['user_birth']);
            try {
                UserWeb::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatus()
    {
        $data = Request::post();
        $model = UserWeb::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['user_status' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemove($id)
    {
        $model = UserWeb::find($id);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}